﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dukou.Javascript
{
    public class JsArithmeticExpr : JsExpr
    {
        public object Left { get; private set; }

        public object Right { get; private set; }

        public JsArithmeticOperator ArithmeticOperator { get; private set; }

        public JsArithmeticExpr(object left, object right, JsArithmeticOperator opt)
        {
            this.Left = left;
            this.Right = right;
            this.ArithmeticOperator = opt;
        }

        public override string ToString()
        {
            string left = this.Left.ToString();
            if (this.Left as JsVar == null)
            {
                left = JsValue.Create(this.Left).ToString();
            }
            if (this.ArithmeticOperator == JsArithmeticOperator.Inc || this.ArithmeticOperator == JsArithmeticOperator.Dec)
            {
                return string.Format("{0}{1};", left, GetOperator(this.ArithmeticOperator));
            }

            string right = this.Right.ToString();
            if (this.Right as JsVar == null)
            {
                right = JsValue.Create(this.Right).ToString();
            }
            return string.Format("{0} {1} {2};", left, GetOperator(this.ArithmeticOperator), right);
        }

        public static string GetOperator(JsArithmeticOperator opt)
        {
            switch (opt)
            {
                case JsArithmeticOperator.Plus:
                    return "+";
                case JsArithmeticOperator.Sub:
                    return "-";
                case JsArithmeticOperator.Mul:
                    return "*";
                case JsArithmeticOperator.Div:
                    return "/";
                case JsArithmeticOperator.Mod:
                    return "%";
                case JsArithmeticOperator.Inc:
                    return "++";
                case JsArithmeticOperator.Dec:
                    return "--";
                default:
                    return "";
            }
        }
    }
}
